package com.woparker.kiosk.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.widget.ImageView;

import com.woparker.kiosk.R;
import com.woparker.kiosk.anim.IconAnimator;
import com.woparker.kiosk.bean.BestPointBean;
import com.woparker.kiosk.bean.MapGroupBean;
import com.woparker.kiosk.bean.NodeBean;
import com.woparker.kiosk.bean.StartEndPointBean;
import com.woparker.kiosk.model.NavigationModel;
import com.woparker.kiosk.utils.SearchTask;
import com.woparker.kiosk.view.MapViewDraw;
import com.woparker.kiosk.view.PathAnimationView;
import com.woparker.kiosk.view.PinchImageView;
import com.woparker.kiosk.view.QSView;
import com.woparker.kiosk.view.mapview.MapView;
import com.woparker.kiosk.view.mapview.layer.RouteLayer;

public class NavigationPresenter {
    private final QSView view;
    private final NavigationModel model;

    //起点人物位置图标
    private Bitmap startBitmap;
    //终点图标
    private Bitmap endBitmap;
    // 路径查询对象
    private SearchTask searchTask;
    //地图
    private MapViewDraw mapViewDraw;

    private PathAnimationView pathAnimView;
    // 起点坐标
    private NodeBean startPoint = new NodeBean(9, 61, null);
    // 终点坐标
    private NodeBean endPoint = new NodeBean(55, 13, null);


    private Context mContext;


    //路线运动图标
    private IconAnimator iconAnimator;


    public NavigationPresenter(Context context, QSView qsView, NavigationModel model){
        this.mContext = context;
        this.view = qsView;
        this.model = model;
    }
    public void setPathAnimView(PathAnimationView pathAnimView) {
        this.pathAnimView = pathAnimView;
    }
    /**
     * 初始化起始点图标
     */
    public void initBitmap(){
        startBitmap = BitmapFactory.decodeResource(mContext.getResources(),
                R.mipmap.map_start).copy(Bitmap.Config.ARGB_8888, true);
        endBitmap = BitmapFactory.decodeResource(mContext.getResources(),
                R.mipmap.map_target).copy(Bitmap.Config.ARGB_8888, true);
    }
    /**
     * 画出最优路线
     * @param sX
     * @param sY
     * @param tX
     * @param tY
     * @param startTips
     * @param floorName
     * @param endTips
     * @param parkingSpaces
     */
    public void drawBestRoot(MapGroupBean mapGroupBean, Canvas canvas, Bitmap bgBitmap,
                             int sX, int sY, int tX, int tY,
                             String startTips, String floorName, String endTips, String parkingSpaces) {
        searchTask = new SearchTask(mapGroupBean);
        mapViewDraw = new MapViewDraw(mContext, canvas, view, bgBitmap, startBitmap,
                endBitmap, searchTask, sY, sX, tX, tY,
                startTips, floorName, endTips, parkingSpaces);
        StartEndPointBean startEndPointBean = searchTask.getBestPoint(sX, sY, tX, tY);
        BestPointBean startXY = startEndPointBean.startPoint;
        BestPointBean targeXY = startEndPointBean.endPoint;
        if (SearchTask.mMapView[startXY.getPointX()][startXY.getPointY()] != 0
                && SearchTask.mMapView[targeXY.getPointX()][targeXY.getPointY()] != 0) {
            startPoint = new NodeBean(startXY.getPointX(), startXY.getPointY(), null);
            endPoint = new NodeBean(targeXY.getPointX(), targeXY.getPointY(), null);
            searchTask.execute(startPoint, endPoint, -1, false, view);
        } else {
            view.onQueryBestRoot(0, 0, null, false);
        }
    }



    /**
     * 获取最优电梯
     *
     * @param startMX
     * @param startMY
     * @param targetMX
     * @param targetMY
     * @param number
     * @param isLast
     */
    public synchronized void getBestFloor(MapGroupBean mapGroupBean, int startMX, int startMY, int targetMX, int targetMY, int number, boolean isLast) {
        searchTask = new SearchTask(mapGroupBean);
        StartEndPointBean startEndPointBean = searchTask.getBestPoint(startMX, startMY, targetMX, targetMY);
        BestPointBean startXY = startEndPointBean.startPoint;
        BestPointBean targeXY = startEndPointBean.endPoint;
        if (SearchTask.mMapView[startXY.getPointX()][startXY.getPointY()] != 0
                && SearchTask.mMapView[targeXY.getPointX()][targeXY.getPointY()] != 0) {
            startPoint = new NodeBean(startXY.getPointX(), startXY.getPointY(), null);
            endPoint = new NodeBean(targeXY.getPointX(), targeXY.getPointY(), null);
            searchTask.execute(startPoint, endPoint,number, isLast, view);
        }
    }


    /**
     * 更新地图UI
     * @param canvas
     */
    public void updataMapUI(Canvas canvas){
        mapViewDraw.getmPath().reset();
        mapViewDraw.onChangeDraw(canvas);
        pathAnimView.init(mapViewDraw.getmPath(),mapViewDraw.getBgBitmap(),mapViewDraw.getfCanvas());
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                pathAnimView.startAnimation();
            }
        });

    }


    private RouteLayer routeLayer;
    public void updataRoot(MapView mapView, ImageView animatorIcon){
        if(mapView.isMapLoadFinish()){
            if(null == routeLayer) {
                routeLayer = new RouteLayer(mapView);
                iconAnimator =  routeLayer.getAnimatorIcon(animatorIcon);
                mapView.addLayer(routeLayer);
            }
            routeLayer.setNodeList(searchTask.getBestRoot());
            mapView.refresh();
            iconAnimator.viewMovePoint(routeLayer.animatorStartX,routeLayer.animatorStartY);
        }
    }

    /**
     * 清除路线
     * @param mapView  地图对象
     */
    public void clearnRoot(MapView mapView){
        if(mapView.isMapLoadFinish()){
            if(null != routeLayer) {
                routeLayer.clearRoute();
                mapView.refresh();
            }
        }
    }


    /**
     * 清除地图路线
     * @param pinchImageView
     */
    public void clearnRootUI(PinchImageView pinchImageView){
        mapViewDraw.clearnBg();
        pinchImageView.reset();
    }


    /**
     * 设置动画
     */
    public void startAnimatorPath() {
        if(null != routeLayer) {
            iconAnimator.startAnimator(1,routeLayer.getAnimatorPath());
        }
    }




    /**
     * 获取地图数组数据
     * @param mapUrl
     */
    public void getMapGroupBean(String mapUrl) {
        view.showLoading("正在获取数据");
    }

    public void getBitmapImage(String imageUrl) {
        view.showLoading("正在获取数据");
    }
}
